iT邦幫忙

2024 iThome 鐵人賽

DAY 6
0
AI/ ML & Data

【AI筆記】30天從論文入門到 Pytorch 實戰系列 第 6

【AI筆記】30天從論文入門到 Pytorch 實戰:資料擴增技術深入探討 Day 5

  • 分享至 

  • xImage
  •  

技術探討

資料增強技術如旋轉、翻轉、調整大小和裁剪經常一起使用來生成更多的訓練資料。另外,請注意,資料增強僅適用於訓練集,而不適用於測試集,因為資料增強旨在增加模型的泛化能力,在測試集上使用會改變測試資料的分布,從而不能正確評估模型的性能。

資料擴增實作

我們先用以下範例去實作

  • 撰寫印出圖片的程式: 根據資料型態去選用顯示的型態,而刀子的磨耗可以用3D方式進行顯示
import numpy as np
import matplotlib.pyplot as plt

def plot_3d(img):
    fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
    # X, Y, Z information
    y, x = img.shape
    X = np.arange(0, x)
    Y = np.arange(0, y)
    X, Y= np.meshgrid(X, Y)
    Z = img
    ax.set_zlim(0, 600) # 自行設定
    # Plot the surface
    surf = ax.plot_surface(
        X, Y, Z,
        cmap='viridis',
        linewidth=0,
        antialiased=False)
    fig.colorbar(surf, shrink=0.5, aspect=8) # 自行設定
    plt.show()
# reference: https://matplotlib.org/stable/gallery/mplot3d/surface3d.html
  • 生成波浪圖像
    用python numpy格式 維度為 1*1000*1000 的 sin(x)數值 帶有平滑波浪形狀 設定範圍在0~300
import numpy as np
import matplotlib.pyplot as plt

def generate_img(n):
  x = np.linspace(0, 4 * np.pi, 1000)
  y = np.linspace(0, 4 * np.pi, 1000)
  X, Y = np.meshgrid(x, y)
  wave = np.random.randint(100,300) + 100 * np.sin(X - np.random.randint(0,n))  # 設定sin(x) 加上隨機高度在100~300以上
  plot_3d(wave)
  return wave
image_ori = generate_img(2)

Sample 圖片

  • 擴增方法: 可以用水平、垂直翻轉、或是轉角度,但因為轉完之後會因轉出去的圖片的角度導致數值為0,所以此方法可能不可用。
import torchvision.transforms as transforms
from PIL import Image

angel = 3
image_new = Image.fromarray(image_ori)
for j in range(10):
    img_aug = transforms.RandomHorizontalFlip()(image_new)
    img_aug = transforms.RandomVerticalFlip()(img_aug)
    img_aug = transforms.RandomRotation( 3 + 2*j )(img_aug)
    plot_3d(np.array(img_aug))

轉角度

可以改用Roll Shift的方式進行擴增。

angel = 3
image_new = Image.fromarray(image_ori)
for j in range(10):
    img_aug = transforms.RandomHorizontalFlip()(image_new)
    img_aug = transforms.RandomVerticalFlip()(img_aug)
    # img_aug = transforms.RandomRotation( 3 + 2*j )(img_aug)
    img_aug = np.roll(np.array(img_aug,dtype=float), shift=20*j,axis=1)
    plot_3d(np.array(img_aug))

Roll Shift 擴增方法

自動化資料擴增技術

自動化資料擴增技術首先由 AutoAugment 提出,該方法自動找出針對目標資料集的最佳資料擴增策略 (policy)。AutoAugment 通過學習選擇的擴增策略,在大多數情況下優於人工挑選的擴增方法及參數。然而使用該方法訓練來選擇擴增方法非常耗費運算資源和時間,因此後來提出了 Fast AutoAugment (Fast AA),該方法使用 Population Based Augmentation (PBA) 的方式,通過基於 population-based training 來搜尋最佳的超參數。Fast AutoAugment 的核心概念是將擴增資料視為訓練資料中的缺失資料,並運用貝氏優化 (Bayesian optimization) 找出合適的策略來恢復這些缺失資料,通過匹配擴增資料與非擴增資料之間的分布來找出最佳的擴增策略。

Data augmentation 清單

此清單方便各位研究時,不用一個個去查還有哪些擴增方法。

Position augmentation

  • [ ] Scaling 縮放
  • [ ] Cropping 裁剪
  • [ ] Flipping 水平翻轉
  • [ ] Padding 填充
  • [ ] Rotation 旋轉
  • [ ] Translation 在平移過程中,圖像沿 x 軸或 y 軸移動。
  • [ ] TranslateX (X平移)
  • [ ] TranslateY (Y平移)
  • [ ] Superpixels 用平均像素顏色和之間的機率值替換每個超像素
  • [ ] RelativeRegularGridVoronoi 在每個圖像上放置一個規則的坐標網格,其中是行數,計算為輸入圖像的高度。是列數,類似地從圖像寬度估計。
  • [ ] 過濾浮雕 pillike.FilterEmboss()
  • [ ] 過濾器輪廓 pillike.FilterContour()
  • [ ] FilterEdgeEnhance (PIL.ImageFilter.SHARPEN / pillike.FilterSharpen())

PIL套件

  • [ ] contour
  • [ ] Emboss
  • [ ] Find Edges

Transforms 套件

  • [ ] transforms.Cutout
  • [ ] transforms.CoarseDropout (BlendAlphaRegularGrid)

Color augmentation

  • [ ] Brightness
  • [ ] Contrast
  • [ ] Saturation
  • [ ] Hue

其他方法:混入雜訊 Additive Noise

Reference

OpenGenus IQ
imgaug套件

Paper Reference

AutoAugment: Learning Augmentation Strategies from Data
Fast AutoAugment


上一篇
【AI筆記】30天從論文入門到 Pytorch 實戰:資料預處理的關鍵步驟 Day 4
下一篇
【AI筆記】30天從論文入門到 Pytorch 實戰:如何選擇合適的模型 Day 6
系列文
【AI筆記】30天從論文入門到 Pytorch 實戰26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言